package com.example.mygame.controller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mygame.R;
import com.example.mygame.pojo.User;
import com.example.mygame.service.SqliteUtils;

import java.util.List;

public class ModifyUserInfo extends AppCompatActivity {

    private EditText nick;
    private EditText age;
    private EditText sex;
    private EditText phone;
    private Button save;

    User finalUser = new User();

    private SqliteUtils sqliteUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_user_info);

        nick = findViewById(R.id.nick);
        age = findViewById(R.id.age);
        sex = findViewById(R.id.sex);
        phone = findViewById(R.id.phone);
        save = findViewById(R.id.save);

        sqliteUtils = new SqliteUtils(this);

        Intent getDataIntent = getIntent();

        String getUserName = getDataIntent.getStringExtra("userName");

        List<User> userList = sqliteUtils.queryFromDbByName(getUserName);


        for (User user : userList) {
            finalUser = user;
            nick.setHint(user.getNick());
            age.setHint(user.getAge());
            sex.setHint(user.getSex());
            phone.setHint(user.getPhone());
        }

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalUser.setNick(nick.getText().toString());
                finalUser.setPhone(phone.getText().toString());
                finalUser.setAge(age.getText().toString());
                finalUser.setSex(sex.getText().toString());
                sqliteUtils.updateData(finalUser);
                Intent intent = new Intent(ModifyUserInfo.this, Mine.class);
                Bundle bundle = new Bundle();
                bundle.putString("userName",finalUser.getName());
                Toast.makeText(getApplicationContext(), "修改成功",
                        Toast.LENGTH_SHORT).show();
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });



    }
}